package com.wteam.dragon.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.dragon.shop.pojo.bean.Address;
import com.wteam.dragon.shop.pojo.vo.AddressVo;
import com.wteam.dragon.shop.service.AddressService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.basebean.vo.PageVo;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 陈晓辉
 * @Date 2021/4/8 16:23
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "地址")
@RequestMapping("address")
public class AddressController {
    private final AddressService addressService;

    @Log("添加地址")
    @ApiOperation("添加地址")
    @PostMapping("addAddress")
    @PreAuthorize("@el.check('addAddress')")
    public ResultMessage addAddress(@Validated AddressVo addressVo){
        Address address = BeanUtil.from(addressVo, Address.class);
        return addressService.addAddress(address);
    }

    @Log("删除地址")
    @ApiOperation("删除地址")
    @DeleteMapping("delAddress")
    @PreAuthorize("@el.check('delAddress')")
    public ResultMessage delAddress(Long id){
        return addressService.delAddress(id);
    }

    @Log("更改地址")
    @ApiOperation("更改地址")
    @PutMapping("editAddress")
    @PreAuthorize("@el.check('editAddress')")
    public ResultMessage editAddress(@Validated AddressVo addressVo){
        Address address = BeanUtil.from(addressVo, Address.class);
        return addressService.editAddress(address);
    }

    @Log("分页获得地址")
    @ApiOperation("分页获得地址")
    @GetMapping("pageAddress")
    @PreAuthorize("@el.check('pageAddress')")
    public ResultMessage pageAddress(PageVo<Address> pageVo){
        Page<Address> page = BeanUtil.fromPage(pageVo);
        return addressService.pageAddress(page);
    }

    @Log("获得地址详情")
    @ApiOperation("获得地址详情")
    @GetMapping("getAddress")
    @PreAuthorize("@el.check('getAddress')")
    public ResultMessage getAddress(Long id){
        return addressService.getAddress(id);
    }

    @Log("设为默认地址")
    @ApiOperation("设为默认地址")
    @PutMapping("defaultAddress")
    @PreAuthorize("@el.check('defaultAddress')")
    public ResultMessage defaultAddress(Long id){
        return addressService.defaultAddress(id);
    }

    @Log("取消设为默认地址")
    @ApiOperation("取消设为默认地址")
    @PutMapping("cancelDefaultAddress")
    @PreAuthorize("@el.check('cancelDefaultAddress')")
    public ResultMessage cancelDefaultAddress(Long id){
        return addressService.cancelDefaultAddress(id);
    }

    @Log("获得默认地址")
    @ApiOperation("获得默认地址")
    @GetMapping("getDefaultAddress")
    @PreAuthorize("@el.check('getDefaultAddress')")
    public ResultMessage getDefaultAddress(){
        return addressService.getDefaultAddress();
    }
}
